package com.funcourse.demo.api.restful.security;

import com.funcourse.demo.api.VersionConfig;
import com.funcourse.demo.wechat.conf.WeletConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TokenFilterConfig {

  @Autowired
  private TokenAuthFilter tokenAuthFilter;

  @Autowired
  private ActivactionFilter activactionFilter;

  @Autowired
  private WechatConfigFilter wechatConfigFilter;

  @Autowired
  private WeletConfigFilter weletConfigFilter;

  @Autowired
  VersionConfig versionConfig;

  @Bean
  public FilterRegistrationBean tokenFilterRegistration() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(tokenAuthFilter);
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/attend/*");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/organize/*");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/common/uploadImage");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/common/getUserInfo");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/common/viewRoomInfoByNumber");
    registration.setName("someFilter");
    registration.setOrder(1);
    return registration;
  }

  @Bean
  public FilterRegistrationBean wechatConfigRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(wechatConfigFilter);
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/login/wechatLogin");
    registration.setOrder(2);
    return registration;
  }

  @Bean
  public FilterRegistrationBean weletConfigRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(weletConfigFilter);
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/login/weletLogin");
    registration.setOrder(3);
    return registration;
  }

  @Bean
  public FilterRegistrationBean activationFilterRegistration() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(activactionFilter);
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/organize/*");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/common/uploadImage");
    registration.addUrlPatterns("/api/"+versionConfig.getVersion()+"/common/viewRoomInfoByNumber");
    registration.setName("activactionFilter");
    registration.setOrder(4);
    return registration;
  }

}
